/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.canvas.MouseHandlers;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.canvas.CanvasModel;
import simplemetrosystem.view.canvas.MetroCanvas;

/**
 *
 * @author Eric Qian
 */
public class NewTextHandler extends BasicMouseHandler {

    public NewTextHandler(MetroFrame frame) {
        super(frame);
        this.frame = frame;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
        frame.setCursor(textCursor);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
        frame.setCursor(defaultCursor);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        CanvasModel model = CanvasModel.getInstance();
        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();
        int x = MouseTrans.X(e.getX());
        int y = MouseTrans.Y(e.getY());

        if (model.isStationAvailable(x, y)) {
            frame.setCursor(textCursor);
            String text = JOptionPane.showInputDialog(
                    NameMap.getInstance().getName("Text") + ":",
                    NameMap.getInstance().getName("Text"));
            EditCommandManager.getInstance().addString(text, null, x, y);
        }
        else if(!model.isEmpty(x, y) && model.isSelectedEmpty(x, y)){
            model.setSelect();
            canvas.setSelectedMouseHandler();
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
        CanvasModel model = CanvasModel.getInstance();
        int x = MouseTrans.X(e.getX());
        int y = MouseTrans.Y(e.getY());

        if (model.isStationAvailable(x, y)) {
            frame.setCursor(textCursor);
        }
    }
    private MetroFrame frame;
    private static Cursor textCursor = new Cursor(Cursor.TEXT_CURSOR);
}
